package com.fxd.maxis
{
	import com.adobe.crypto.MD5;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.http.HTTPService;

	public class FinderAPI extends EventDispatcher
	{
		private const API_key:String = "a50e1a8a-a010-4bf3-ad0c-7ffac86a4089";
		private var secret_key:String = "abae276b-21e3-437d-97f6-11c64a954a7a";
		private var signature_id:String;
		private var session_key:String = "";
		private var call_id:String = "1309851924079";
		private var baseURL:String = "http://api.finder301.com";
		private var d:Date;
		public var error_code:String;
		public var error_msg:String;
		
		public var cat_id:String;
		public var sub_id:String;
		public var poi_id:String;
		
		public var categories:ArrayCollection;
		public var subCategories:ArrayCollection;
		public var pois:ArrayCollection;
		public var poi:Object;
		public var srv:HTTPService;
		
		public function FinderAPI()
		{
			srv = new HTTPService();
			srv.method = "GET"; 
			srv.addEventListener("result", resultHandler); 
			srv.addEventListener("fault", faultHandler);
			getSession();
		}
		private function getSession():void{
			d = new Date();
			call_id = d.time.toString();
			signature_id = MD5.hash("/v1/session" + "api_key=" + API_key + "call_id=" + call_id + secret_key);
			srv.url = baseURL + "/v1/session" +"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id;
			srv.send(); 
		}
		private function resultHandler(evt:Event):void{ 
			for (var i:String in srv.lastResult) 
			{
				trace("Response: "+i)
				switch (i){
					case "session_response":
						session_key = srv.lastResult.session_response.session_key;
						dispatchEvent(new Event("START_SESSION"));
						break;
					case "categories_response":
						categories = ArrayCollection(srv.lastResult.categories_response.categories.category);
						dispatchEvent(new Event("GET_CATEGORIES"));
						break;
					case "error_response":
						error_code = srv.lastResult.error_response.error_code;
						error_msg = srv.lastResult.error_response.error_msg;
						trace("Error:"+srv.lastResult.error_response.error_msg)
						dispatchEvent(new Event("FAULT"));
						break;
					case "pois_response":
						if(srv.lastResult.pois_response.pois){
							pois = ArrayCollection(srv.lastResult.pois_response.pois.poi);
							dispatchEvent(new Event("GET_POI_LIST"));
						}else{
							subCategories = ArrayCollection(srv.lastResult.pois_response.sub_categories.sub_category);
							dispatchEvent(new Event("GET_SUB_CATEGORIES"));
						}
						break;
					case "poi_response":
						poi = srv.lastResult.poi_response.poi;
						dispatchEvent(new Event("GET_POI_DETAIL"));
						break;
					default:
						//http://api.finder301.com/v1/search/pois?api_key=56328615-d5a7-45bf-a66f-6aea516752a6&call_id=1310059588523&sig=f4c5ade1d319c713097becc9bf7edc6c&session_key=YWJjZVkyTlhjOWZmWmdCeExPZmV0&category=rv&page=1&size=20&x=3.052882&y=101.687616&radius=20000&keyword=&scid=900000002
						trace("unknown")
						break;
				}
			};
		}
		private function faultHandler(evt:Event):void{
			dispatchEvent(new Event("FAULT"));
		}
		public function getCategories():void{
			d = new Date();
			if(session_key.length>0){
				call_id = d.time.toString();
				signature_id = MD5.hash("/v1/categories" + "api_key=" + API_key + "call_id=" + call_id + secret_key);
				srv.url = baseURL + "/v1/categories" +"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id + "&session_key=" + session_key;
				srv.send();
			}else{
				getSession();
			}
		}
		public function getSubCategories(_cat_id:String, lat:Number, lng:Number, _page:int = 1, _size:int = 20, _radius:int = 20000, _sub_category:Boolean = true):void{
			d = new Date();
			cat_id = _cat_id;
			if(session_key.length>0){
				call_id = d.time.toString();
				signature_id = MD5.hash("/v1/search/pois" + "api_key=" + API_key + "call_id=" + call_id + secret_key);
				srv.url = baseURL + "/v1/search/pois" +"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id + "&session_key=" + session_key +
					"&category=" + cat_id + "&page=" + _page + "&size=" + _size + "&x=" + lat + "&y=" + lng + "&radius=20000" + _radius +
					"&keyword=&sub_category=true";
				srv.send();
			}
		}
		public function getPOIList(_cat_id:String, lat:Number, lng:Number, _page:int = 1, _size:int = 20, _radius:int = 20000, _scid:String = ""):void{
			d = new Date();
			cat_id = _cat_id;
			sub_id = _scid;
			if(session_key.length>0){
				call_id = d.time.toString();
				signature_id = MD5.hash("/v1/search/pois" + "api_key=" + API_key + "call_id=" + call_id + secret_key);
				if(sub_id == ""){
					srv.url = baseURL + "/v1/search/pois" +"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id + "&session_key=" + session_key +
						"&category=" + cat_id + "&page=" + _page + "&size=" + _size + "&x=" + lat + "&y=" + lng + "&radius=20000" + _radius +
						"&keyword=";
				}else{
					srv.url = baseURL + "/v1/search/pois" +"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id + "&session_key=" + session_key +
						"&category=" + cat_id + "&page=" + _page + "&size=" + _size + "&x=" + lat + "&y=" + lng + "&radius=20000" + _radius +
						"&keyword=&scid="+sub_id;
				}
				srv.send();
			}
		}
		public function getPOIDetail(_poi_id:String, _cat_id:String):void{
			d = new Date();
			cat_id = _cat_id;
			poi_id = _poi_id;
			if(session_key.length>0){
				call_id = d.time.toString();
				signature_id = MD5.hash("/v1/poi/"+poi_id + "api_key=" + API_key + "call_id=" + call_id + secret_key);
				srv.url = baseURL + "/v1/poi/"+ poi_id+"?api_key=" +API_key+"&call_id="+call_id+"&sig="+signature_id + "&session_key=" + session_key +
					"&category=" + cat_id ;
				srv.send();
			}
		}
	}
}